package com.pin.zheng.xingmulibrary.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.pin.zheng.xingmulibrary.R;
import com.pin.zheng.xingmulibrary.util.Utils;

/**
 * @Description: 描述 更新APP弹窗
 * @Author: zyzh
 * @Date: 2020/01/13
 * @Time: 11:00
 */
public class CommomUpdataDialog extends Dialog implements View.OnClickListener {
    private Context mContext;
    private String content, title, ok, no;
    private OnCloseListener listener;
    private boolean isShow = true;

    public CommomUpdataDialog(@NonNull Context context) {
        super(context);
    }

    public CommomUpdataDialog(@NonNull Context context, int themeResId, String content, OnCloseListener listener) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
        this.listener = listener;
    }

    public CommomUpdataDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected CommomUpdataDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_commom_updata);
        setCanceledOnTouchOutside(false);
        initView();
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setOk(String ok) {
        this.ok = ok;
    }

    public void setNo(String no, boolean isShow) {
        this.no = no;
        this.isShow = isShow;
    }

    private void initView() {
        TextView tv_queding = findViewById(R.id.tv_queding);
        TextView tv_quxiao = findViewById(R.id.tv_quxiao);
        TextView tv_title = findViewById(R.id.tv_title);
        TextView tv_content = findViewById(R.id.tv_content);
        tv_queding.setOnClickListener(this);
        tv_quxiao.setOnClickListener(this);
        tv_content.setGravity(Gravity.LEFT);

        if (!TextUtils.isEmpty(content)) {
            tv_content.setText(content);
        }
        if (!TextUtils.isEmpty(title)) {
            tv_title.setText(title);
        }
        if (!TextUtils.isEmpty(ok)) {
            tv_quxiao.setText(ok);
        }
        if (isShow) {
            tv_queding.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(no)) {
                tv_queding.setText(no);
            }
        } else {
            tv_queding.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        if (!Utils.isFastClick(v.getId())) {
            return;
        }
        int id = v.getId();
        if (id == R.id.tv_queding) {
            if (listener != null) {
                listener.onClick(this, false);
            }
            this.dismiss();
        } else if (id == R.id.tv_quxiao) {
            if (listener != null) {
                listener.onClick(this, true);
            }
            this.dismiss();
        }
    }

    public interface OnCloseListener {
        void onClick(Dialog dialog, boolean confirm);
    }
}
